package com.xpsoft.oa.workflow.event;

import java.math.BigDecimal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.admin.AdminContract;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.admin.AdminContractService;
import com.xpsoft.oa.service.flow.ProcessRunService;

public class AdminContractFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(AdminContractFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String content = (String)pi.getVariable("adminContract.content");
//		String contractNo = (String)pi.getVariable("adminContract.contractNo");
		String ourRepresent = (String)pi.getVariable("adminContract.ourRepresent");
		String secondPartyUnit = (String)pi.getVariable("adminContract.secondPartyUnit");
		String contractForm = (String)pi.getVariable("adminContract.contractForm");
		BigDecimal contractAmount = (BigDecimal)pi.getVariable("adminContract.contractAmount");
		String payType = (String)pi.getVariable("adminContract.payType");
		String appointment = (String)pi.getVariable("adminContract.appointment");
		Integer originalCopyNum = (Integer)pi.getVariable("adminContract.originalCopyNum");
		Integer giveCopyNum = (Integer)pi.getVariable("adminContract.giveCopyNum");
		String attachIds = (String)pi.getVariable("adminContract.attachIds");
		String attachFiles = (String)pi.getVariable("adminContract.attachFiles");
		
		AdminContractService adminContractService = (AdminContractService)AppUtil.getBean("adminContractService");
		AdminContract adminContract = new AdminContract();
		adminContract.setContent(content);
//		adminContract.setContractNo(contractNo);
		adminContract.setOurRepresent(ourRepresent);
		adminContract.setSecondPartyUnit(secondPartyUnit);
		adminContract.setContractForm(contractForm);
		adminContract.setContractAmount(contractAmount);
		adminContract.setPayType(payType);
		adminContract.setAppointment(appointment);
		adminContract.setOriginalCopyNum(originalCopyNum);
		adminContract.setGiveCopyNum(giveCopyNum);
		adminContract.setAttachFiles(attachFiles);
		adminContract.setAttachIds(attachIds);
		adminContract.setContractNo(adminContractService.getNewContractNo());
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		adminContract.setProcessRunId(processRun.getRunId());
		
		adminContractService.save(adminContract);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
